import * as mobx from 'mobx';
import {observable, action, useStrict, computed, asMap} from 'mobx';
import {Bubble,Tools} from "weaver-mobile";
import * as authTypes from "../constants/secondAuthType";

const getLabel = Tools.getLabel;
const ls = Tools.ls;

export class SecondAuthStore {
    /**
     * 二次验证成功后，确认时，调用的流程提交方法
     */
    callbackFunc = null;

    /**
     *  流程提交时的参数
     */
    callbackParam = {};

    /**
     * 是否需要二次验证
     */
    isEnableAuth = 0;

    /**
     * 二次验证的类型
     */
    secondAuthType = 0;

    /**
     *  是否启用了数据保护
     */
    isEnableProtect = 0;

    /**
     *  数据保护的类型
     */
    protectType = 0;

    /**
     * 1为使用契约锁Ukey，2为使用契约锁服务
     */
    qysSignWay = 2;
    /**
     * 二次验证成功
     */
    isAuthSuccess = 0;

    /**
     * 保护数据成功
     */
    isProtectSuccess = 0;

    /**
     *  0：表示二次验证的窗口
     *  1：表示是CA数据保护
     *  10：表示是CA认证和数据保护一起弹出的窗口
     */
    isProtectDialog = 0;

    /**
     * CA认证时的key
     */
    authKey = "";

    /**
     *  数据保护时需要加密的源数据
     */
    sourcestr = "";

    @observable
    dialogVisible = observable.map ? observable.map({}) : asMap({});    //弹窗显示属性

    /**
     * 是否是提交到退回节点
     */
    isSubmitDirect = 0;

    /**
     *  契约锁验证时的打开的窗口的地址(地址契约锁提供，调用契约锁的接口获取)
     */
    @observable
    signUrl = "";


    /**
     *  验证的结果，主要是存储契约锁认证的结果
     *  sealId,sealImg
     */
    authResult = {};

    /**
     * 是否清除操作按钮
     */
    clearButten = false;

    constructor() {
        this.setCallbackFunc = this.setCallbackFunc.bind(this);
        this.setCallbackParam = this.setCallbackParam.bind(this);
        this.setIsSubmitDirect = this.setIsSubmitDirect.bind(this);
        this.controlDialogVisible = this.controlDialogVisible.bind(this);
        this.setDialogData = this.setDialogData.bind(this);
        this.setDialogResult = this.setDialogResult.bind(this);
        this.saveDataToStorage = this.saveDataToStorage.bind(this);
        this.readDataFromStorage = this.readDataFromStorage.bind(this);
        this.resetStore = this.resetStore.bind(this);

        this.callbackFunc = () => {
        };
        this.callbackParam = {};
    }

    @action
    setCallbackFunc(func) {
        this.callbackFunc = func;
    }

    /**
     *  设置二次验证后提交流程时的参数
     *  这个是从流程提交时的方法中设置的
     */
    @action("验证成功后提交流程的参数")
    setCallbackParam(param) {
        this.callbackParam = {...this.callbackParam, ...param};
    }

    setIsSubmitDirect(val = 0) {
        this.isSubmitDirect = val;
    }

    canDoSecondAuth(){
        let canDo = true;
        if(navigator.userAgent.indexOf("E-Mobile7") < 0){
            if(this.isEnableAuth == 1&&this.secondAuthType == authTypes.QYS&&this.qysSignWay == 1){
                canDo = false;
            }else if(this.isEnableProtect == 1&& this.protectType == authTypes.QYS&&this.qysSignWay == 1){
                canDo = false;
            }else if(this.isEnableAuth == 1&&this.secondAuthType == authTypes.CA_AUTH){
                canDo = false;
            }else if(this.isEnableProtect == 1&&this.protectType == authTypes.CA_AUTH){
                canDo = false;
            }
            if(!canDo){
                Bubble.warning(getLabel(517171, "请使用新版EMobile进行操作!"));//517171
            }
        }
        return canDo;
    }

    @action
    controlDialogVisible(key, value) {
        this.dialogVisible.set(key, value);
    }

    @action("验证或者数据保护的窗口需要的数据")
    setDialogData(data) {
        /**
         * isEnableAuth, secondAuthType, isEnableProtect, protectType,signUrl参数的值
         */
        Object.assign(this, data);
    }

    @action("设置CA、契约锁认证或者保护时的数据")
    setCAAuthData(data) {
        /**
         * 设置isProtectDialog,authKey,sourcestr参数的值
         */
        Object.assign(this, data);
    }

    @action("设置验证或者数据保护的结果")
    setDialogResult(data) {
        /**
         * 设置isAuthSuccess,isProtectSuccess参数的值
         */
        Object.assign(this, data);
    }

    @action
    setAuthResult(obj) {
        this.authResult = {...this.authResult, ...obj};
    }

    @action("将数据存入localStorage")
    saveDataToStorage(authKey) {
        const callbackParam = this.callbackParam;
        const isEnableAuth = this.isEnableAuth;
        const secondAuthType = this.secondAuthType;
        const isEnableProtect = this.isEnableProtect;
        const protectType = this.protectType;
        const isAuthSuccess = this.isAuthSuccess;
        const isProtectSuccess = this.isProtectSuccess;
        const isProtectDialog = this.isProtectDialog;
        const isSubmitDirect = this.isSubmitDirect;
        const obj = {
            callbackParam,
            isEnableAuth,
            secondAuthType,
            isEnableProtect,
            protectType,
            isAuthSuccess,
            isProtectSuccess,
            isProtectDialog,
            isSubmitDirect
        };

        ls.set(authKey, JSON.stringify(obj));
    }

    @action("从localStorage中读取数据")
    readDataFromStorage(authKey) {
        const obj = ls.getJSONObj(authKey);
        const {
            callbackParam,
            isEnableAuth,
            secondAuthType,
            isEnableProtect,
            protectType,
            isAuthSuccess,
            isProtectSuccess,
            isProtectDialog,
            isSubmitDirect
        } = obj;

        this.callbackParam = callbackParam;
        this.isEnableAuth = isEnableAuth;
        this.secondAuthType = secondAuthType;
        this.isEnableProtect = isEnableProtect;
        this.protectType = protectType;
        this.isAuthSuccess = isAuthSuccess;
        this.isProtectSuccess = isProtectSuccess;
        this.isProtectDialog = isProtectDialog;
        this.isSubmitDirect = isSubmitDirect;
    }

    @action("重置所有值为初始化")
    resetStore() {
        this.callbackFunc = () => {
        };
        this.callbackParam = {};
        this.isEnableAuth = 0;
        this.secondAuthType = 0;
        this.isEnableProtect = 0;
        this.protectType = 0;
        this.isAuthSuccess = 0;
        this.isProtectSuccess = 0;
        this.isProtectDialog = 0;
        this.isSubmitDirect = 0;
        this.authResult = {};
    }
}